﻿#include <Configuration.h>
#include "NewColorSchemeDialog.h"
#include "ui_NewColorSchemeDialog.h"

namespace Domy {

NewColorSchemeDialog::NewColorSchemeDialog(const QString &name, QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::NewColorSchemeDialog),
    m_all_scheme_names(domy_config->getAllSchemeNames())
{
    m_ui->setupUi(this);

    m_ui->lineEdit_name->setText(name);

    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);

    // lineEdit设置文件名的正则表达式
    QRegExp reg("[^/\\\\<>*?|\"]+\\.[^/\\\\<>*?|\"]+");
    m_ui->lineEdit_name->setValidator(new QRegExpValidator(reg, this));

    connect(m_ui->pushButton_ok, SIGNAL(clicked()), this, SLOT(slotOnOKBtnClicked()));
    connect(m_ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close()));
    connect(m_ui->lineEdit_name, SIGNAL(textChanged(const QString &)),
            this, SLOT(slotOnNameChanged(const QString &)));
}

NewColorSchemeDialog::~NewColorSchemeDialog()
{
    delete m_ui;
}

void NewColorSchemeDialog::slotOnNameChanged(const QString &name)
{
    static QPalette origin_pal = m_ui->lineEdit_name->palette();

    if (m_all_scheme_names.contains(name)) {
        QPalette pal = m_ui->lineEdit_name->palette();
        pal.setColor(QPalette::Text, Qt::red);
        m_ui->lineEdit_name->setPalette(pal);
    }
    else {
        m_ui->lineEdit_name->setPalette(origin_pal);
    }
}

void NewColorSchemeDialog::slotOnOKBtnClicked()
{
    QString name = m_ui->lineEdit_name->text();
    if (name.isEmpty()) {
        return;
    }

    emit signalNewSchemeName(name);

    this->close();
}

}
